package commons.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet Filter implementation class FilterEncoding
 */
@WebFilter(filterName = "filterEncoding", urlPatterns = {"*.main.java.action"})
public class FilterEncoding implements Filter {

    /**
     * Default constructor.
     */
    public FilterEncoding() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Filter#destroy()
     */
    public void destroy() {
        // TODO Auto-generated method stub
    }

    /**
     * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
     */
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        // 类型转换
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        // 处理中文乱码
        // 处理post请求的中文乱码
        req.setCharacterEncoding("utf-8");
        // 处理响应的字符集中文乱码
        resp.setCharacterEncoding("utf-8");
        // 处理浏览器字符集中文乱码
        resp.setContentType("text/html;charset=utf-8");
        chain.doFilter(request, response);
    }

    /**
     * @see Filter#init(FilterConfig)
     */
    public void init(FilterConfig fConfig) throws ServletException {
    }

}
